if(SIMULATOR STREQUAL "vcs")

	# Find verilator

	if(DEFINED ENV{VCS_HOME})
		set(VCS_HOME $ENV{VCS_HOME})
		message(STATUS "Find VCS root: ${VCS_HOME}")
	else()
		message(FATAL_ERROR "Cannot find vcs, please install (vcs/O-2018.09-SP2)")
	endif()

	# Add verilator include path for build
	include_directories(${VCS_HOME}/include ${VCS_HOME}/linux64/lib/)
	add_definitions(-DUSE_VCS)
	add_link_options(
		-Wl,-rpath=${VCS_HOME}/linux64/lib
		-L${VCS_HOME}/linux64/lib
		-no-pie
		-Wl,--no-as-needed
		-rdynamic
		-Wl,-whole-archive
		-lvcsucli
		-Wl,-no-whole-archive
		-lzerosoft_rt_stubs
		-luclinative
		-lvirsim
		-lerrorinf
		-lsnpsmalloc
		-lvfs
		-lvcsnew
		-lsimprofile
		-ldl
		-lc
		-lm
		-lpthread
		-lnuma)
	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,stack-size=1048576")

	add_library(vcs_tls OBJECT IMPORTED)
	set_target_properties(vcs_tls PROPERTIES IMPORTED_OBJECTS
																					 ${VCS_HOME}/linux64/lib/vcs_tls.o)
	add_library(vcs_save_restore_new OBJECT IMPORTED)
	set_target_properties(
		vcs_save_restore_new
		PROPERTIES IMPORTED_OBJECTS ${VCS_HOME}/linux64/lib/vcs_save_restore_new.o)

	if(TRACE STREQUAL "ON")
		message(STATUS "Tracing waves is ON")
		add_definitions(-DVL_TRACE)
	else()
		message(STATUS "Tracing waves is OFF")
	endif()

	if(NOT WAVE_FORMAT STREQUAL "FSDB")
		message(FATAL_ERROR "Only FSDB is supported for VCS")
	endif()

	if(COVERAGE STREQUAL "ON")
		message(STATUS "Coverage is NOT SUPPOERTED for VCS YET")
		add_definitions(-DVL_COVERAGE)
	else()
		message(STATUS "Coverage is OFF")
	endif()

	if(SIM_MULTI_THREAD STREQUAL "ON")
		message(STATUS "Multi-threading is NOT SUPPOERTED for VCS YET")
		add_definitions(-DVL_THREADED)
	else()
		message(STATUS "Multi-threading is OFF")
	endif()

	# DEFINE mcv VCS gen function
	function(mcv_gen_vcs_dut UTFileName UTModuleName UTSourceModuleName
					 UTSourceFilePath VcsFlags)

		if(EXISTS ${PROJECT_SOURCE_DIR}/dut/${UTFileName}/vc_hdrs.h)
			message(STATUS "Skip build dut/${UTFileName}")
			return()
		endif()
		execute_process(
			COMMAND rm -rf ${PROJECT_SOURCE_DIR}/dut/${UTFileName}
			COMMAND rm -rf ${PROJECT_SOURCE_DIR}/dut/${UTFileName}_trace)

		if(WAVE_FORMAT STREQUAL "FSDB")
			set(WAVE_NAME "${UTModuleName}.fsdb")
		else()
			message(FATAL_ERROR "WAVE_FORMAT must be FSDB ")
		endif()

		include_directories(${PROJECT_SOURCE_DIR}/dut/${UTFileName})

		if (NOT VcsFlags STREQUAL "") 
			SET(VcsFlags "-V ${VcsFlags}")
		endif()

		execute_process(
			WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/mcv
			COMMAND rm -rf ${PROJECT_SOURCE_DIR}/dut/${UTFileName}
			COMMAND
				./build/bin/mcv -f ${UTSourceFilePath} -s ./template/xdut/cpp -t
				${PROJECT_SOURCE_DIR}/dut/${UTFileName} -S ${UTSourceModuleName} -T
				${UTModuleName} --sim vcs ${VcsFlags}
			COMMAND rm -rf ${PROJECT_SOURCE_DIR}/dut/${UTFileName}_trace
			COMMAND
				./build/bin/mcv -f ${UTSourceFilePath} -s ./template/xdut/cpp -t
				${PROJECT_SOURCE_DIR}/dut/${UTFileName}_trace -S ${UTSourceModuleName}
				-T ${UTModuleName} ${VcsFlags} -w ${WAVE_NAME} --sim vcs)
		if(NOT EXISTS ${PROJECT_SOURCE_DIR}/dut/${UTFileName})
			message(FATAL_ERROR "Cannot generate ${UTFileName}")
		endif()

		execute_process(WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/dut/${UTFileName}
										COMMAND make)
		execute_process(
			WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/dut/${UTFileName}_trace
			COMMAND make)

	endfunction()
endif()
